<?php
namespace Admin\Controller;

use Think\Controller;
use Org\Util\Rbac;
use Think\Crypt;

/**
 * 后台基类
 *
 * @author ryan
 *        
 */
class BaseController extends Controller
{

    function _initialize()
    {
        if (C('USER_AUTH_ON') && ! in_array(CONTROLLER_NAME, explode(',', C('NOT_AUTH_MODULE')))) {
            if (! Rbac::AccessDecision()) {
                if (! $_SESSION[C('USER_AUTH_KEY')]) {
                    $this->error('请先登录', C('USER_AUTH_GATEWAY'));
                }
                if (C('RBAC_ERROR_PAGE')) {
                    $this->error('您没有权限操作该项');
                }
            }
        }
    }

    function getUserInfo()
    {
        $user_info = $_SESSION['user_info'];
        $user = json_decode(Crypt::decrypt($user_info, C('USER_AUTH_KEY')), true);
        if (is_array($user)) {
            unset($user['password']);
        }
        return $user;
    }

    public function upload($rootPath, $savePath)
    {
        $config = array(
            'maxSize' => 3145728,
            'rootPath' => $rootPath,
            'savePath' => $savePath,
            'saveName' => array(
                'uniqid'
            ),
            'exts' => array(
                'jpg',
                'gif',
                'png',
                'jpeg'
            ),
            'thumb' => true,
            'subName' => array(),
            'autoSub' => true
        );
        $upload = new \Think\Upload($config); // 实例化上传类
        if (isset($_FILES)) {
            $info = $upload->upload();
            return $info;
        } else {
            return null;
        }
    }
}

